#include "printmemberpassword.h"
#include "ui_printmemberpassword.h"
#include <QMessageBox>
#include <QKeyEvent>

PrintMemberPassword::PrintMemberPassword(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PrintMemberPassword)
{
    ui->setupUi(this);
    this->setFixedSize(384,120);
    setWindowTitle(tr("请输入会员密码"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    //消除边框
    ui->pushButtonComfirm->setFlat(true);

    //添加时间过滤器
    ui->lineEditPassword->installEventFilter(this);
    ui->pushButtonComfirm->installEventFilter(this);
}

PrintMemberPassword::~PrintMemberPassword()
{
    delete ui;
}

void PrintMemberPassword::set_memberCardNumber(QString memberCardNumber)
{
    m_memberCardNumber = memberCardNumber;
    ui->lineEditPassword->setFocus();
    ui->lineEditPassword->setText(NULL);
}

void PrintMemberPassword::closeEvent(QCloseEvent *)
{
    emit clearMemberInformation();
}

bool PrintMemberPassword::eventFilter(QObject *target, QEvent *event)
{
    if ( target == ui->lineEditPassword || target == ui->pushButtonComfirm )
    {
        //键盘事件
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->lineEditPassword)
                {
                    on_pushButtonComfirm_clicked();
                    return true;
                }
            }
        }
    }
    return QWidget::eventFilter(target, event);
}

void PrintMemberPassword::on_pushButtonComfirm_clicked()
{
    if (m_memberPassword == ui->lineEditPassword->text())
    {
        emit displayMemberInformation(m_memberCardNumber);
        this->hide();
    }
    else
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n会员密码错误,请重新输入!"));
        ui->lineEditPassword->setFocus();
        ui->lineEditPassword->setText(NULL);
    }
}
